package com.hspedu.map_;

import java.util.*;

/**
 * @author LiuJinTao
 * @className MapFor
 * @date 2024/06/09 23:43
 * @description TODO
 */
@SuppressWarnings({"all"})
public class MapFor {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("邓超", "孙俪");
        map.put("王宝强", "马蓉");
        map.put("宋喆", "马蓉");
        map.put("刘令博", null);
        map.put(null, "刘亦菲");
        map.put("鹿晗", "关晓彤");

        // 1.先取出所有的 key，通过 key 取出对应的 value
        Set keySet = map.keySet();
        // 遍历 Set
        // (1)增强for
        System.out.println("====第一种方式====");
        for (Object key : keySet) {
            System.out.println(key + "-" + map.get(key));
        }
        // (2)迭代器
        System.out.println("====第二种方式====");
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key =  iterator.next();
            System.out.println(key + "-" + map.get(key));
        }

        // 2.把所有的 values 取出
        Collection values = map.values();
        // (1)增强for
        System.out.println("====第三种方式====");
        for (Object vlaue : values) {
            System.out.println(values);
        }
        // (2)迭代器
        System.out.println("====第四种方式====");
        Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object next =  iterator1.next();
            System.out.println(next);
        }

        // 3.通过 EntrySet 来获取
        Set entrySet = map.entrySet();// EntrySet<Map.Entry<K, V>>
        // (1)增强for
        System.out.println("====第五种方式====");
        for (Object entry : entrySet) {
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "-" + m.getValue());
        }
        // (2)迭代器
        System.out.println("====第六种方式====");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object next = iterator2.next();
//            System.out.println(next.getClass());// HashMap$Node 实现了 Map.Entry
            Map.Entry m = (Map.Entry) next;
            System.out.println(m.getKey() + "-" + m.getValue());
        }
    }
}
